package com.jrh.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jrh.biz.siteMsg.SiteMsgService;
import com.jrh.biz.user.UserService;
import com.jrh.dal.UserProfileDao;
import com.jrh.domain.common.Page;
import com.jrh.domain.entity.SiteMsg;
import com.jrh.domain.entity.User;
import com.jrh.domain.entity.UserProfile;
import com.jrh.domain.user.LoginUserInfo;
import com.jrh.web.member.UserForm;
import com.jrh.web.member.tools.UserAuthUtil;

/**
 * 类UserController.java的实现描述：用户中心
 * 
 * @author mmyjse@gmail.com 2012-8-5 上午3:14:21
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final Integer pageSize = 10;

    @Autowired
    private SiteMsgService       siteMsgService;

    @Autowired
    private UserService          userService;

    @Autowired
    private UserProfileDao       userProfileDao;

    @RequestMapping(method = RequestMethod.GET, value = "/mebak")
    public String me(Model model) {
        return "user/index";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/siteMsg")
    public String listSiteMsg(Integer pageIndex, Model model, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Integer start = (pageIndex - 1) * pageSize;
        Page<SiteMsg> list = siteMsgService.findAllByLoginId(userInfo.getLoginId(), start, pageSize);
        model.addAttribute("list", list);
        return "user/siteMsg";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/siteMsg/delete")
    public String delete(@RequestParam(value = "id", required = true) String id, Model model) {
        String[] idsStr = id.split(",");
        Long[] ids = new Long[idsStr.length];
        for (int i = 0; i < idsStr.length; i++) {
            ids[i] = Long.parseLong(idsStr[i]);
        }
        boolean isSuccess = siteMsgService.deleteSiteMsg(ids);
        if (isSuccess) {
            model.addAttribute("success", true);
            return "redirect:/user/siteMsg";
        } else {
            return "error";
        }
    }

    @RequestMapping(method = RequestMethod.GET, value = "/security")
    public String security(Model model, HttpServletRequest request, HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo == null) {
            return "error";
        }
        model.addAttribute("user", userInfo);

        UserProfile userProfile = userProfileDao.getUserProfileByUserId(userInfo.getId());
        model.addAttribute("userProfile", userProfile);
        return "user/security";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/security")
    public String doSecurity(@RequestParam(value = "oldPassword", required = true) String oldPassword,
                             @RequestParam(value = "newPassword", required = true) String newPassword,
                             @ModelAttribute("userProfile") UserForm userForm, Model model, HttpServletRequest request,
                             HttpServletResponse response) {
        LoginUserInfo userInfo = UserAuthUtil.getUserInfo(request, response);
        if (userInfo == null) {
            return "error";
        }
        User user = new User();
        user.setId(userInfo.getId());
        user.setLoginId(userInfo.getLoginId());
        user.setPassword(oldPassword);
        boolean success = userService.updatePassword(user, newPassword);
        if (success) {
            model.addAttribute("success", Boolean.TRUE);
            UserProfile userProfile = new UserProfile();
            userProfile.setId(userForm.getUserProfileId());
            userProfile.setUserId(userInfo.getUserId());
            userProfile.setQuestion(userForm.getQuestion());
            userProfile.setAnswer(userForm.getAnswer());
            userService.updateUserProfile(userProfile);
            return "user/security";
        } else {
            model.addAttribute("success", Boolean.FALSE);
            model.addAttribute("errorMsg", "修改用户个人信息错误");
            return "error";
        }
    }
}
